var fs = require('fs');
var mysql = require('mysql');
var superagent = require('superagent');
var cheerio = require('cheerio');

require('superagent-proxy')(superagent);

var mysqlConfig1 = require('./config').mysqlConfig1;
var mysqlConfig2 = require('./config').mysqlConfig2;

var ipList = [];
var ipIndex = 0;
var proxy = '';

// var connection1 = mysql.createConnection(mysqlConfig2);
// var query1 = 'select * from ip_table1';
// connection1.connect();
// connection1.query(query1, function (err, res) {
//     if (err) {
//         console.log('query1 error');
//     } else {
//         ipList = res;
//         // console.log(ipList.length);
//         // setProxy();
//         // console.log(proxy);

        
//     }
// });
// connection1.end();

fn1(1001, 0);
fn1(1002, 0);
fn1(1003, 0);
fn1(2001, 0);
fn1(2002, 0);
fn1(2003, 0);
fn1(6001, 0);
fn1(6002, 0);
fn1(6003, 0);
fn1(7001, 0);
fn1(7002, 0);
fn1(7003, 0);
fn1(4001, 0);
fn1(4002, 0);
fn1(4003, 0);




// https://music.163.com/#/discover/artist/cat?id=1001&initial=-1
function fn1(typeId, initial) {
    var artistList = [];
    var url = `https://music.163.com/discover/artist/cat?id=${typeId}&initial=${initial}`;
    console.log(url);

    superagent.get(url)
        // .set()
        .proxy(proxy)
        .end(function (err, res) {
            // var time1 = (new Date).getTime();
            // console.log(typeId);
            // console.log(time1 - time0);

            let content = res.res.text;
            
            fs.writeFile(`page/${typeId}-${initial}.html`, content, function () {
                // 
            });
            // if(initial<90){
            //     fn1(typeId, initial + 1);
            // }else{
            //     console.log('end');
                
            // }
            let $ = cheerio.load(content);

            let list = $('.m-sgerlist>ul>li .s-fc0');
            // console.log(list.length);

            let values = []
            list.each((i, ele) => {
                let element = list.eq(i);
                // 歌手名
                let name = element.text();
                // console.log(name);
                let id = element.attr().href.replace(/.*id./g, '');
                let cat = typeId;
                // let hot = 
                let value = [id, name, cat];
                values.push(value);
                
            });
            console.log(values);

            // if(typeId==='1001'){

            //     // console.log(values);

            // }

            let connection = mysql.createConnection(mysqlConfig1);
            let queryString = `insert ignore into artist(id,name,cat) values ?`;
            connection.query(queryString, [values], function (error, results) {
                if (error) {
                    console.log(error);
                }
                if (results) {
                    // let time2 = (new Date).getTime();
                    console.log('inserted');
                    // console.log(typeId);

                    // console.log(time2 - time0);
                    // console.log();

                }

            })
            connection.end();
        })
}

function changeIp() {
    if (ipIndex < ipList.length) {
        ipIndex += 1;
    } else {
        ipIndex = 0;
    }
}

function setProxy() {
    var item = ipList[ipIndex];
    console.log(item);

    var ip = item.ip;
    var port = item.port;

    proxy = `http://${ip}:${port}`;
    console.log(proxy);

}